package com.liao.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liao.common.entity.product.CategoryEntity;
import com.liao.common.utils.PageUtils;
import com.liao.common.utils.Query;
import com.liao.product.dao.CategoryDao;
import com.liao.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listALL() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        return entities;
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);

        //组装成父子树形结构
        List<CategoryEntity> level1Menus = entities.stream().
                filter(entity -> entity.getParentId() == 0).
                map(entity -> {
                    entity.setChildren(getChildren(entity, entities));
                    return entity;
                }).
                sorted((entity1, entity2) ->
                        (entity1.getSortOrder() == null ? 0 : entity1.getSortOrder()) -
                                (entity2.getSortOrder() == null ? 0 : entity2.getSortOrder())).
                collect(Collectors.toList());
        return level1Menus;
    }

    @Override
    public List<Integer> getIdListById(Integer categoryId) {
        //查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);

        List<Integer> idList = new ArrayList<>();

        Integer parentCategoryId = categoryId;
        while (parentCategoryId != null && !parentCategoryId.equals(0)) {
            idList.add(0, parentCategoryId);
            parentCategoryId = getParentCategoryId(parentCategoryId, entities);
        }

        return idList;
    }

    @Override
    public boolean hasChild(Integer categoryId) {
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<>();
        categoryEntityQueryWrapper.eq("parent_id", categoryId);
        int count = baseMapper.selectCount(categoryEntityQueryWrapper);
        return count > 0;
    }

    @Override
    public List<CategoryEntity> getMutli() {
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<>();
        categoryEntityQueryWrapper.eq("parent_id", 0);
        List<CategoryEntity> entities = baseMapper.selectList(categoryEntityQueryWrapper);
        return entities;
    }

    /**
     * 递归获取分类下的子分类
     *
     * @param root 要获取子分类的分类
     * @param all  所有分类数据
     * @return 子分类列表
     */
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> childerList = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().equals(root.getCategoryId());
        }).map(categoryEntity -> {
//            找到子菜单
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted(
//                排序
                (m1, m2) ->
                        (m1.getSortOrder() == null ? 0 : m1.getSortOrder()) - (m2.getSortOrder() == null ? 0 : m2.getSortOrder())

        ).collect(Collectors.toList());
        return childerList;
    }

    /**
     * 获取当但分类的父分类id
     *
     * @param CategoryId
     * @param all
     * @return 成功返回id，失败返回null
     */
    private Integer getParentCategoryId(Integer CategoryId, List<CategoryEntity> all) {

        for (CategoryEntity categoryEntity : all) {
            if (categoryEntity.getCategoryId().equals(CategoryId)) {
                return categoryEntity.getParentId();
            }
        }
        return null;
    }


}